﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Services;
using RCREEE;
using RCREEE.Library;
using RCREEE.Library.Entities;

namespace RCREEE.Service
{
    public partial class Service
    {

        /// <summary>
        /// Gets All Contacts for specific entity, country & LanguageCode
        /// </summary>
        /// <param name="entityId">Entity Id</param>
        /// <param name="languageCode">Language Code: 1025 = Ar-SA , 1033 = Eng-US , 1036 = FR-Fr</param>
        /// <param name="countryId">Country Id</param>
        /// <returns>List of all Contacts matching the given parameters</returns>
        [WebMethod]
        public List<Contact> GetAllContacts(int entityId, int languageCode, int countryId, out List<string> errorMessages)
        {
            #region Validation

            // validate for valid parametes before passing them to the Business layer

            errorMessages = new List<string>();

            if (entityId <= 0)
                errorMessages.Add("Please, Provide a valid EntityId");

            if (languageCode != 1025 && languageCode != 1033 && languageCode != 1036)
                errorMessages.Add("Invalid Language Code, Must be one of these: 1025, 1033 OR 1036");

            if (countryId <= 0)
                errorMessages.Add("Please, provide a valid CountryId");

            if (errorMessages.Count > 0)
                return null;

            #endregion

            // create a temp object form Contact calss
            Contact _contact = new Contact();

            // cretae a list to hold the return value
            List<Contact> _LContacts = new List<Contact>();

            // Get Data from Conatct class
            _LContacts = _contact.GetAllContactsForWS(entityId, languageCode, countryId);

            // if return value isn't null
            if (_LContacts != null)
                return _LContacts;
            else
            {
                // if an error occured ... set error message with a propriate message
                errorMessages.Add("An error has occured!");
                return null;
            }
        }

        /// <summary>
        /// Get one contact by Contact Id & Language Code
        /// </summary>
        /// <param name="contactId">Contact Id</param>
        /// <param name="languageCode">Language Code: 1025= Ar-SA, 1033 = En-US, 1036= Fr-FR</param>
        /// <param name="errorMessages">list of strings to report errors if exist</param>
        /// <returns>Contact data object contains all available data</returns>
        [WebMethod]
        public Contact GetOneContactById(int contactId, int languageCode, out List<string> errorMessages)
        {
            #region Validation

            errorMessages = new List<string>();

            // check for valid contact Id
            if (contactId <= 0)
                errorMessages.Add("Please, Provide a valid Contact Id");

            // check for valid Language code
            if (languageCode != 1025 && languageCode != 1033 && languageCode != 1036)
                errorMessages.Add("Invalid Language Code, Must be one of these: 1025, 1033 OR 1036");

            #endregion

            // if not valid parameters .. return
            if (errorMessages.Count > 0)
                return null;

            // create and fill Contact Object
            Contact _tempContact = new Contact();
            _tempContact = _tempContact.GetOneContractByIdForWS(contactId, languageCode);

            if (_tempContact != null)
                return _tempContact;
            else
            {
                errorMessages.Add("An error has occurred!");
                return null;
            }
        }

        /// <summary>
        /// Save Contact object in Database
        /// </summary>
        /// <param name="_contact">Contact Object</param>
        /// <returns>Success State</returns>
        [WebMethod]
        public bool SaveContact(Contact contact, out List<string> errorMessages)
        {
            #region Validation

            errorMessages = new List<string>();

            if (String.IsNullOrEmpty(contact.ContactName))
                errorMessages.Add("Must provide a Name for the current Contact");

            if (contact.CountryId <= 0)
                errorMessages.Add("Please, provide a valid country Id");

            if (contact.EntityId <= 0)
                errorMessages.Add("Please, provide a valid Entity Id");

            if (contact.FieldId <= 0)
                errorMessages.Add("Please, provide a valid Field Id");

            if (contact.LanguageCode != 1025 && contact.LanguageCode != 1033 && contact.LanguageCode != 1036)
                errorMessages.Add("Invalid Language Code, Must be one of these: 1025, 1033 OR 1036");

            #endregion

            // if errors exist .. return
            if (errorMessages.Count > 0)
                return false;

            bool saveSuccessful = false;
            // Switch between Update or Insert depending on ContactId
            if (contact.ContactId == 0)
                saveSuccessful = contact.InsertContact(contact);
            else
                saveSuccessful = contact.UpdateContact(contact);

            // if saving wasn't performed successfuly .. Add an error message
            if (!saveSuccessful)
                errorMessages.Add("An error has occured during saving contact data");

            // retunt saving succcess state
            return saveSuccessful;

        }

        /// <summary>
        /// Deletes a contact data -for a specific language code- from database
        /// </summary>
        /// <param name="contactId">Contact Id</param>
        /// <param name="languageCode">Language Code: 1025 = Er-SA, 1033 = En-US, 1036 = Fr-FR</param>
        /// <param name="errorMessages"></param>
        /// <returns>Success state</returns>
        [WebMethod]
        public bool DeleteContact(int contactId, int languageCode, out List<string> errorMessages)
        {

            #region Validation
            errorMessages = new List<string>();

            if (contactId <= 0)
                errorMessages.Add("Please, Provide a valid contact Id to be deleted");

            if (languageCode != 1025 && languageCode != 1033 && languageCode != 1036)
                errorMessages.Add("Invalid Language Code, Must be one of these: 1025, 1033 OR 1036");

            #endregion

            bool deleteSuccessful = false;

            Contact _contact = new Contact();

            _contact.ContactId = contactId;
            _contact.LanguageCode = languageCode;

            deleteSuccessful = _contact.DeleteContact(_contact);

            if (!deleteSuccessful)
                errorMessages.Add("An error has occured during deleteing the current contact");

            return deleteSuccessful;
        }


        [WebMethod]
        public string GetEnergyConsumptionItemValue(int itemId, int year, int countryId, out List<string> errorMessages)
        {

            #region Validation
            errorMessages = new List<string>();

            if (itemId <= 0)
                errorMessages.Add("Please, provide a valid Item Id");

            if (year <= 0)
                errorMessages.Add("Please, Provide a valid Year");

            if (countryId <= 0)
                errorMessages.Add("Please, Provide a valid Country Id");

            #endregion

            if (errorMessages.Count > 0)
                return "";

            return "";
        }
    }
}